package com.fenghongzhang.day005;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class RefreshLayout extends FrameLayout {

    private static final String TAG = "RefreshLayout";
    //头部
    private LinearLayout mRefreshLayout;

    //刷新的高度
    private int mMaxRefreshHeight = 200;

    //点
    private PointF mBeforePointF;

    public RefreshLayout(@NonNull Context context) {
        super(context);
        initView();
    }

    public RefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    //初始化的操作
    private void initView() {
        Log.i(TAG, "initView: ");
        mBeforePointF = new PointF();
        mRefreshLayout = new LinearLayout(getContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mMaxRefreshHeight);
        layoutParams.topMargin = -mMaxRefreshHeight;
        mRefreshLayout.setLayoutParams(layoutParams);
        mRefreshLayout.setBackgroundColor(Color.RED);
        addView(mRefreshLayout);
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            mBeforePointF.x = ev.getX(); //200
            mBeforePointF.y = ev.getY();//200
        }else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            //偏移量
            float xDimension = Math.abs(ev.getX() - mBeforePointF.x);
            float yDimension = Math.abs(ev.getY() - mBeforePointF.y);
            //判读滑动方向
            if (xDimension > yDimension * 2) {
                //X轴滑动
                return super.dispatchTouchEvent(ev);
            }
            //最小响应距离
            if (yDimension < 5) {
                return super.dispatchTouchEvent(ev);
            }
            //可滑动范围
            //Main下拉
            FrameLayout.LayoutParams mainParams = (LayoutParams) mRefreshLayout.getLayoutParams();
            mainParams.topMargin += ev.getY() - mBeforePointF.y;

            if (mainParams.topMargin > 0) {
                mainParams.topMargin = 0;
            }
////
            if (mainParams.topMargin < -mMaxRefreshHeight) {
                mainParams.topMargin = -mMaxRefreshHeight;
            }

            mRefreshLayout.setLayoutParams(mainParams);
            mBeforePointF.x = ev.getX();
            mBeforePointF.y = ev.getY();
        }else if (ev.getAction() == MotionEvent.ACTION_UP) {
            //当滑动位置，打开或关闭
            LayoutParams mainParams = (LayoutParams) mRefreshLayout.getLayoutParams();
            if (mainParams.topMargin < -mMaxRefreshHeight / 2) {
                startAnim(mainParams.topMargin, -mMaxRefreshHeight);
            } else {
                startAnim(mainParams.topMargin, 0);

            }
        }
        return true;

    }
    private void startAnim(int start, int end) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.setDuration(500);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                FrameLayout.LayoutParams mainParams = (LayoutParams) mRefreshLayout.getLayoutParams();
                mainParams.topMargin = (int) animation.getAnimatedValue();
                mRefreshLayout.setLayoutParams(mainParams);
//                requestLayout();
            }
        });

        valueAnimator.start();
    }
}
